
package combat.dudes.server.protocol.implementation;

import java.rmi.RemoteException;
import java.util.ArrayList;

import combat.dudes.DudesFunctions;
import combat.dudes.protocol.data.RemoteList;
import combat.dudes.protocol.interfaces.ProjectileInt;
import combat.dudes.protocol.interfaces.TeamInt;
import combat.dudes.protocol.interfaces.TrackInt;
import combat.dudes.protocol.interfaces.UserInt;
import combat.dudes.protocol.interfaces.VehicleInt;
import combat.dudes.server.ServerApp;


/**
 * The team class, containing all data of a team: users, vehicles, tracks and projectiles (all PhysicalObjects).
 * @author Laurens Andriessen
 * @author Gerard Krombeen
 * @author Jesper Keijzer
 * @author Erik Meijer
 * @version 1.0
 */
public class Team extends NewID.TeamID implements TeamInt {

	private static final long serialVersionUID = 1L;

	private int teamID = -1;
	private String teamName = "";
	private ArrayList<UserInt> users = new ArrayList<UserInt>();
	private ArrayList<VehicleInt> vehicles = new ArrayList<VehicleInt>();
	private ArrayList<TrackInt> tracks = new ArrayList<TrackInt>();
	private ArrayList<ProjectileInt> projectiles = new ArrayList<ProjectileInt>();

	public Team() {
		this.teamID = getID();
	}
	public Team(String teamName) {
		this.teamID = getID();
		this.teamName = teamName;
	}
	public Team(int teamID, String teamName) {
		this.teamID = teamID;
		this.teamName = teamName;
	}


	@Override
	public int getTeamID() {
		return teamID;
	}
	@Override
	public void setTeamID(int teamID) {
		this.teamID = teamID;
	}

	@Override
	public String getTeamName() {
		return teamName;
	}
	@Override
	public void setTeamName(String teamName) {
		this.teamName = teamName;
	}

	@Override
	public ArrayList<UserInt> getUsers() {
		return users;
	}
	@Override
	public void setUsers(ArrayList<UserInt> users) {
		this.users = users;
	}
	// Helpers
	@Override
	public void addUser(UserInt user) {
		if(DudesFunctions.Network.isProxy(user)) user = Simulation.instance().findUser(user);
		users.add(user);
		ServerApp.Log("User '" + user.getUserName() + "' added to team '" + this.teamName + "'");
	}
	@Override
	public void removeUser(UserInt user) {
		users.remove(Simulation.instance().findUser(user));
		ServerApp.Log("User '" + user.getUserName() + "' removed from team '" + this.teamName + "'");
	}

	@Override
	public ArrayList<VehicleInt> getVehicles() {
		return vehicles;
	}
	@Override
	public void setVehicles(ArrayList<VehicleInt> vehicles) {
		this.vehicles = vehicles;
	}
	// Helpers
	@Override
	public void addVehicle(VehicleInt vehicle) {
		if(DudesFunctions.Network.isProxy(vehicle)) vehicle = (VehicleInt) Simulation.instance().findPhysicalObject(vehicle);
		this.vehicles.add(vehicle);
		ServerApp.Log("Vehicle '" + vehicle.getName() + "' added to team '" + this.teamName + "'");
	}
	@Override
	public void removeVehicle(VehicleInt vehicle) {
		vehicles.remove(vehicle);
	}

	@Override
	public ArrayList<TrackInt> getTracks() {
		return tracks;
	}
	@Override
	public void setTracks(ArrayList<TrackInt> tracks) {
		this.tracks = tracks;
	}
	// Helpers
	@Override
	public void addTrack(TrackInt track) {
		tracks.add(track);
	}
	@Override
	public void removeTrack(TrackInt track) {
		tracks.remove(track);
	}

	@Override
	public ArrayList<ProjectileInt> getProjectiles() {
		return projectiles;
	}
	@Override
	public void setProjectiles(ArrayList<ProjectileInt> projectiles) {
		this.projectiles = projectiles;
	}
	// Helpers
	@Override
	public void addProjectile(ProjectileInt projectile) {
		projectiles.add(projectile);
	}
	@Override
	public void removeProjectile(ProjectileInt projectile) {
		projectiles.remove(projectile);
	}

	@Override
	public RemoteList getUsers_Remote() {
		try {
			return new RemoteList(getUsers());
		} catch(RemoteException e) {
			ServerApp.onError("@Team.java, getUsers_Remote()", e, false);
		}
		return new RemoteList();
	}

	@Override
	public RemoteList getVehicles_Remote() {
		try {
			return new RemoteList(getVehicles());
		} catch(RemoteException e) {
			ServerApp.onError("@Team.java, getVehicles_Remote()", e, false);
		}
		return new RemoteList();
	}

	@Override
	public RemoteList getTracks_Remote() {
		try {
			return new RemoteList(getTracks());
		} catch(RemoteException e) {
			ServerApp.onError("@Team.java, getTracks_Remote()", e, false);
		}
		return new RemoteList();
	}

	@Override
	public RemoteList getProjectiles_Remote() {
		try {
			return new RemoteList(getProjectiles());
		} catch(RemoteException e) {
			ServerApp.onError("@Team.java, getProjectiles_Remote()", e, false);
		}
		return new RemoteList();
	}
}
